package unwdmi.listener;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import unwdmi.connector.ClientConnector;
import unwdmi.connector.StationConnector;
/**
 * This class creates a listener that listens for clients that want to connect to the server
 * @author Stephan E G Veenstra
 *
 */
public class ClientListener implements Runnable {

	private ServerSocket serverSocket;
	private ExecutorService executor;
	
	public ClientListener(ServerSocket serverSocket){
		this.serverSocket = serverSocket;
		this.executor = Executors.newCachedThreadPool();
	}
	
	@Override
	public void run() {
		System.out.println("Listening for Clients to connect...");
		Socket clientSocket;
		
		while(true){
			try {
				// Listen for connecting WeatherStations
				clientSocket = serverSocket.accept();
				// Start a new thread for the connection
				executor.execute(new ClientConnector(clientSocket));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
